﻿#include "GUICurvePlotSettingsDialog.h"
#include "ui_GUICurvePlotSettingsDialog.h"

#include "FITK_Component/FITKPlotWindow/FITKXYPlotWindow.h"
#include "FITK_Component/FITKPlotWindow/FITKXYPlotProperty.h"
#include "FITK_Component/FITKPlotWindow/FITKXYCurveDrawManager.h"
#include <QTableWidget>
#include <QHeaderView>
#include <cmath>

namespace GUI
{
    GUICurvePlotSettingsDialog::GUICurvePlotSettingsDialog(QWidget *parent) :
        Core::FITKDialog(parent),
        _ui(new Ui::GUICurvePlotSettingsDialog)
    {
        _ui->setupUi(this);
        // 去掉帮助按钮
        setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
        //初始化窗口
        this->initWidget();
    }

    GUICurvePlotSettingsDialog::~GUICurvePlotSettingsDialog()
    {
        delete _ui;
    }

    void GUICurvePlotSettingsDialog::init(std::vector<float>* freqValue, Plot::FITKXYPlotWindow* curvePlotWidget)
    {
        //初始化频率下拉框
        this->initFreqComboBox(freqValue);
        //初始化x，y轴的数据
        this->initCurvePlotSettings(curvePlotWidget);
    }

    void GUICurvePlotSettingsDialog::getFreqRange(double* range)
    {
        if (!range) return;
        //获取起始和终止频率
        range[0] = _ui->comboBox_StartFreq->currentData().toDouble();
        range[1] = _ui->comboBox_EndFreq->currentData().toDouble();
    }

    void GUICurvePlotSettingsDialog::getYAxisCalcValue(bool& aWeight, double& dB)
    {
        aWeight = _ui->checkBox_AWeighting->checkState() == Qt::CheckState::Checked;
        dB = -1.0;
        if (_ui->groupBox_DB->isChecked())
        {
            if (_ui->radioButton_UserDefind->isChecked())
                dB = _ui->lineEdit_UserDefind->text().toDouble();
            else if (_ui->radioButton_Default->isChecked())
                dB = _ui->lineEdit_Default->text().toDouble();
        }
    }

    void GUICurvePlotSettingsDialog::updateCurvePlot(Plot::FITKXYPlotWindow* curvePlotWidget)
    {
        if (!curvePlotWidget) return;
        //从界面获取数据
        double xAxisMin = 0.0, xAxisMax = 0.0, yAxisMin = 0.0, yAxisMax = 0.0;
        bool isXAxisLog = false, isYAxisLog = false;
        xAxisMin = _ui->lineEdit_xAxisMin->text().toDouble();
        xAxisMax = _ui->lineEdit_xAxisMax->text().toDouble();
        yAxisMin = _ui->lineEdit_yAxisMin->text().toDouble();
        yAxisMax = _ui->lineEdit_yAxisMax->text().toDouble();
        isXAxisLog = _ui->radioButton_xAxisLog->isChecked();
        isYAxisLog = _ui->radioButton_yAxisLog->isChecked();
        //获取曲线配置
        Plot::FITKXYPlotProperty* propCurvePlot = curvePlotWidget->getPlotProperty();
        if (!propCurvePlot) return;
        if (!_ui->checkBox_xAxisAutoscale->isChecked())
            propCurvePlot->setAxisScale(2, xAxisMin, xAxisMax);
        else
            propCurvePlot->setAxisScale(2, 0, 0);
        if (!_ui->checkBox_yAxisAutoscale->isChecked())
            propCurvePlot->setAxisScale(0, yAxisMin, yAxisMax);
        else
            propCurvePlot->setAxisScale(0, 0, 0);
        propCurvePlot->setAxisScaleEngine(2, isXAxisLog);
        propCurvePlot->setAxisScaleEngine(0, isYAxisLog);
    }

    void GUICurvePlotSettingsDialog::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }

    void GUICurvePlotSettingsDialog::on_pushButton_OK_clicked()
    {
        this->accept();
    }

    void GUICurvePlotSettingsDialog::on_groupBox_DB_clicked(bool checked)
    {
        if (checked)
            _ui->radioButton_yAxisLinear->setChecked(true);
        _ui->radioButton_yAxisLog->setEnabled(!checked);
        _ui->radioButton_yAxisLinear->setEnabled(!checked);
    }

    void GUICurvePlotSettingsDialog::on_comboBox_StartFreq_currentIndexChanged(int index)
    {
        //屏蔽信号发出
        _ui->comboBox_EndFreq->blockSignals(true);
        //获取开始频率，终止频率
        double startFreq = _ui->comboBox_StartFreq->currentText().toDouble();
        double endFreq = _ui->comboBox_EndFreq->currentText().toDouble();
        //开始频率要小于终止频率
        if (endFreq < startFreq)
            _ui->comboBox_EndFreq->setCurrentIndex(index);
        //恢复信号发出
        _ui->comboBox_EndFreq->blockSignals(false);
    }

    void GUICurvePlotSettingsDialog::on_comboBox_EndFreq_currentIndexChanged(int index)
    {
        //屏蔽信号发出
        _ui->comboBox_StartFreq->blockSignals(true);
        //获取开始频率，终止频率
        double startFreq = _ui->comboBox_StartFreq->currentText().toDouble();
        double endFreq = _ui->comboBox_EndFreq->currentText().toDouble();
        //开始频率要小于终止频率
        if (endFreq < startFreq)
            _ui->comboBox_StartFreq->setCurrentIndex(index);
        //恢复信号发出
        _ui->comboBox_StartFreq->blockSignals(false);
    }

    void GUICurvePlotSettingsDialog::on_checkBox_xAxisAutoscale_stateChanged(int state)
    {
        _ui->lineEdit_xAxisMin->setEnabled(state == Qt::CheckState::Checked ? false : true);
        _ui->lineEdit_xAxisMax->setEnabled(state == Qt::CheckState::Checked ? false : true);
    }

    void GUICurvePlotSettingsDialog::on_checkBox_yAxisAutoscale_stateChanged(int state)
    {
        _ui->lineEdit_yAxisMin->setEnabled(state == Qt::CheckState::Checked ? false : true);
        _ui->lineEdit_yAxisMax->setEnabled(state == Qt::CheckState::Checked ? false : true);
    }

    void GUICurvePlotSettingsDialog::on_radioButton_Default_clicked(bool state)
    {
        _ui->lineEdit_Default->setEnabled(state);
        _ui->lineEdit_UserDefind->setEnabled(!state);
    }

    void GUICurvePlotSettingsDialog::on_radioButton_UserDefind_clicked(bool state)
    {
        _ui->lineEdit_UserDefind->setEnabled(state);
        _ui->lineEdit_Default->setEnabled(!state);
    }

    void GUICurvePlotSettingsDialog::initWidget()
    {
        _ui->groupBox_5->setVisible(false);
        _ui->groupBox_7->setVisible(false);
        _ui->groupBox_8->setVisible(false);
        _ui->tabWidget->setTabEnabled(3, false);
        _ui->tabWidget->setTabEnabled(4, false);
    }

    void GUICurvePlotSettingsDialog::initFreqComboBox(std::vector<float>* freqValue)
    {
        if (!freqValue || freqValue->size() == 0) return;
        //清理下拉框
        _ui->comboBox_StartFreq->clear();
        _ui->comboBox_EndFreq->clear();
        //设置下拉框的数据
        for (std::vector<float>::iterator iter = freqValue->begin(); iter != freqValue->end(); ++iter)
        {
            _ui->comboBox_StartFreq->addItem(QString::number(*iter), *iter);
            _ui->comboBox_EndFreq->addItem(QString::number(*iter), *iter);
        }
        //初始化下拉框
        _ui->comboBox_StartFreq->setCurrentIndex(0);
        _ui->comboBox_EndFreq->setCurrentIndex(_ui->comboBox_EndFreq->count() - 1);
    }

    void GUICurvePlotSettingsDialog::initCurvePlotSettings(Plot::FITKXYPlotWindow* curvePlotWidget)
    {
        if (!curvePlotWidget) return;
        _ui->lineEdit_Default->setText("1");
        _ui->lineEdit_UserDefind->setText("1");
        //获取曲线配置
        Plot::FITKXYPlotProperty* propCurvePlot = curvePlotWidget->getPlotProperty();
        if (!propCurvePlot) return;
        double xAxisMin = 0.0, xAxisMax = 0.0, yAxisMin = 0.0, yAxisMax = 0.0;
        propCurvePlot->getAxisScale(2, xAxisMin, xAxisMax);
        propCurvePlot->getAxisScale(0, yAxisMin, yAxisMax);
        bool isXAxisLog = false, isYAxisLog = false;
        isXAxisLog = propCurvePlot->isAxisScaleEngine(2);
        isYAxisLog = propCurvePlot->isAxisScaleEngine(0);
        //设置配置界面
        _ui->lineEdit_xAxisMin->setText(QString::number(xAxisMin));
        _ui->lineEdit_xAxisMax->setText(QString::number(xAxisMax));
        _ui->lineEdit_yAxisMin->setText(QString::number(yAxisMin));
        _ui->lineEdit_yAxisMax->setText(QString::number(yAxisMax));
        if (isXAxisLog)
            _ui->radioButton_xAxisLog->setChecked(true);
        else
            _ui->radioButton_xAxisLinear->setChecked(true);
        if (isYAxisLog)
            _ui->radioButton_yAxisLog->setChecked(true);
        else
            _ui->radioButton_yAxisLinear->setChecked(true);
    }
}
